﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Editform1 : Form
    {
        public Editform1()
        {
            InitializeComponent();
           
        }

        public Editform1(int id, string title, string author, string createdTime)
        {
            InitializeComponent();
            this.id = id;
            textBox1.Text = title;
            textBox2.Text = author;
            textBox3.Text = createdTime;
        }

        private int id;

        //确认保存
        private void button1_Click(object sender, EventArgs e)
        {

            var title = textBox1.Text;
            var author = textBox2.Text;
            var createdTime = textBox3.Text;

            if (id>0)
            {
                if (!string.IsNullOrEmpty(title) && !string.IsNullOrEmpty(author) && !string.IsNullOrEmpty(createdTime))
                {
                    var sql = string.Format("update Articles set Title = '{0}',Author='{1}',CreatedTime='{2}' where Id = {3}", title, author, createdTime,this.id);
                    var res = DbHelper.AddOrEditOrDeleteSave(sql);
                    if (res == 1)
                    {
                        MessageBox.Show("更新成功", "提示");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();

                    }
                    else
                    {
                        MessageBox.Show("更新失败", "提示");

                    }
                }
                else
                {
                    MessageBox.Show("信息不完善", "提示");
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(title)&& !string.IsNullOrEmpty(author)&& !string.IsNullOrEmpty(createdTime))
                {
                    var sql = string.Format("insert into Articles (Title,Author,CreatedTime) values ('{0}','{1}','{2}')",title,author,createdTime);
                    var res = DbHelper.AddOrEditOrDeleteSave(sql);
                    if (res == 1)
                    {
                        MessageBox.Show("添加成功", "提示");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();

                    }
                    else
                    {
                        MessageBox.Show("添加失败", "提示");

                    }
                }
                else
                {
                    MessageBox.Show("信息不完善","提示");
                }
            }
        }

        //取消
        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();
        }
    }
}
